import types from './types'
import lodash from 'lodash'

export default {
  [types.SET_USER] (state, payload) {
    state.user = payload
  },
  [types.SET_USERS] (state, payload) {
    state.users = payload
  },
  [types.ADD_USER] (state, payload) {
    state.users.items.push(payload)
    state.users.total ++
  },
  [types.DELETE_USER] (state, payload) {
    let index = lodash.findIndex(state.users.items, ['username', payload])
    if (index >= 0) {
      state.users.items.splice(index, 1)
    }
  },
  [types.SET_USER_ROLES] (state, payload) {
    state.roles = payload
  },
  [types.ADD_USER_ROLE] (state, payload) {
    state.roles.push(payload)
  },
  [types.DELETE_USER_ROLE] (state, payload) {
    let index = lodash.findIndex(state.roles, ['id', payload])
    if (index >= 0) {
      state.roles.splice(index, 1)
    }
  }
}
